using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

public class MyClass
{
	const string alpha = "abcdefghijklmnopqrstuvwxyz";
	const string num = "1234567890";
	const string folder = @"C:\Users\chris\Documents\COP5725\Project\tack-tracker";
	const int startId = 0;
	const int users = 150;
	const int PROJ_CNT = 500;
	const int TASK_CNT = 1500;
	static int[] TASK_PRIOR = new int[]{ 1,2,3 };
	const int TIME_CNT = 10000;
	
	static Random rand = new Random();
	
	public static void RunSnippet()
	{		
		//System.Diagnostics.Debugger.Launch();
		
		if(!Directory.Exists(folder)){
			throw new Exception("Directory not found " + folder);
		}
				
		using(TextWriter writer = new StreamWriter(Path.Combine(folder, "Data.sql"))) {
			//generate delete
			writer.WriteLine(@"
DELETE FROM TASKTIME;
DELETE FROM TASK;
DELETE FROM PROJECT;
DELETE FROM SESSIONS;
DELETE FROM USERS;");
			
			writer.WriteLine();
			
			int id = startId;			
			
			//Users
			int user_min_id = id + 1;
			for(int i = 0; i < users; i++) {
				id++;
				
				string[] udatearr = CreateDateArr();
				
				writer.WriteLine(String.Format(
@"INSERT INTO USERS (UserId, Email, Pwd, RoleId, Created, Modified)
VALUES ({0}, '{1}', '{2}', {3}, to_date('{4}', 'mm/dd/yyyy hh24:mi:ss'), to_date('{5}', 'mm/dd/yyyy hh24:mi:ss'));",
				id, CreateEmail(), CreatePwd(), rand.Next(1,4), udatearr[0], udatearr[1]));
			}
			int user_max_id = id;
			
			int project_min_id = id + 1;
			//Projects			
			for(int i = 0; i < PROJ_CNT; i++) {
				id++;
				
				string[] pdatearr = CreateDateArr();

				writer.WriteLine(String.Format(
@"INSERT INTO Project (ProjectId, Name, Description, Created, Modified)
VALUES ({0}, 'Project {1}', 'Description of project {1}', to_date('{2}', 'mm/dd/yyyy hh24:mi:ss'), to_date('{3}', 'mm/dd/yyyy hh24:mi:ss'));",
				id, i + 1, pdatearr[0], pdatearr[1]));
			}
			int project_max_id = id;
			
			//Tasks
			int task_min_id = id + 1;
			for(int i = 0; i < TASK_CNT; i++) {
				id++;
				
				string[] tdatearr = CreateDateArr();

				writer.WriteLine(String.Format(
@"INSERT INTO Task (TaskId, ProjectId, AssignedUserId, Name, Description, TaskPriorityId, EstimatedHours, Created, Modified)
VALUES ({0},{1},{2},'Project {3}','Description of project {3}',{4},{5},to_date('{6}', 'mm/dd/yyyy hh24:mi:ss'), to_date('{7}', 'mm/dd/yyyy hh24:mi:ss'));",
				id, rand.Next(project_min_id, project_max_id+1), rand.Next(user_min_id, user_max_id), i+1, TASK_PRIOR[rand.Next(0, TASK_PRIOR.Length)], rand.Next(1, 2000), tdatearr[0], tdatearr[1]));
			}
			int task_max_id = id;
			
			//Task Time
			for(int i = 0; i < TIME_CNT; i++) {
				id++;
				
				string[] timedatearr = CreateDateArr();

				writer.WriteLine(String.Format(
@"INSERT INTO TaskTime (TaskTimeId, TaskId, UserId, Hours, Description, Created, Modified)
VALUES ({0},{1},{2},{3},'{4}',to_date('{5}', 'mm/dd/yyyy hh24:mi:ss'), to_date('{6}', 'mm/dd/yyyy hh24:mi:ss'));",
				id, rand.Next(task_min_id, task_max_id+1), rand.Next(user_min_id, user_max_id+1), rand.Next(1,2000), CreateString(10, 600), timedatearr[0], timedatearr[1]));
			}
		}
	}

	private static DateTime CreateDate() {  
		int month = rand.Next(1, 13);
		int year = 2011;
		int hour = rand.Next(0, 24);
		int min = rand.Next(0, 60);
		int sec = rand.Next(0, 60);
		
		DateTime dt = new DateTime(year, month, 1, hour, min, sec);
		
		int days = DateTime.DaysInMonth(year, month);
		dt.AddDays(rand.Next(0, days+1));
		return dt;
	}
	
	private static string[] CreateDateArr() { 
		var s = CreateDate();
		var e = CreateDate();
		if(s > e) { 
			var temp = s;
			s = e;
			e = temp;
		}
		return new string[]{s.ToString("MM/dd/yyyy HH:mm:ss"), e.ToString("MM/dd/yyyy HH:mm:ss")};
	} 

	private static string CreateEmail() { 
		string[] suffix = new string[]{"com", "net", "gov", "org", "edu", "biz", "info"};
		string email = CreateString(5, 21);
		return email + "@" + suffix[rand.Next(0, suffix.Length)];
	}

	private static string CreatePwd() { 
		return "test";
	}
	
	private static string CreateString(int minlen, int maxlen) { 
		string result = "";
		
		string all = alpha + num;
		
		if(minlen > maxlen) { 
			throw new InvalidOperationException();
		} 
		
		int len = minlen;
		if(minlen < maxlen) { 
			len = rand.Next(minlen, maxlen);
		}
		
		int x = all.Length;
		for(int i = 0; i < len; i++) { 
			result += all[rand.Next(0, x)];
		}
		
		return result;
	}
	#region Helper methods
	
	public static void Main()
	{
		try
		{
			RunSnippet();
		}
		catch (Exception e)
		{
			string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
			Console.WriteLine(error);
		}
		finally
		{
			Console.Write("Press any key to continue...");
			Console.ReadKey();
		}
	}

	private static void WL(object text, params object[] args)
	{
		Console.WriteLine(text.ToString(), args);	
	}
	
	private static void RL()
	{
		Console.ReadLine();	
	}
	
	private static void Break() 
	{
		System.Diagnostics.Debugger.Break();
	}

	#endregion
}